package com.rajaev.dtu.tractor.domain.repository.redis;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public class DeviceGPSRedisRepository {

    @Autowired
    RedisTemplate redisTemplate;

    private static final String KEY = "DEVICE:GPS";

    public void saveDeviceGPSInfo(String vin, double lat, double lng) {
        Point point = new Point(lat, lng);
        redisTemplate.opsForGeo().add(KEY, point, vin);
    }


    public Optional<Point> getDeviceGps(String vin){
        List<Point> list = redisTemplate.opsForGeo().position(KEY,vin);
       return CollectionUtils.isEmpty(list)|| list.get(0)==null?Optional.empty():Optional.of(list.get(0));
    }
}
